﻿#pragma execution_character_set("utf-8")
#include "applicationitem.h"
#include "ui_applicationitem.h"

ApplicationItem::ApplicationItem(const QString &email,const QString &username,const QString &content,QWidget *parent) :
    QWidget(parent),
    email(email),
    ui(new Ui::ApplicationItem)
{
    ui->setupUi(this);
    ui->username_LB->setText(username);
    ui->content_LB->setText(content);
    updateHeadImg();
    connect(ui->agree_BT,&QPushButton::clicked,[this](){
        emit agreeApplication(this->email,ui->username_LB->text());
    });
}

ApplicationItem::~ApplicationItem()
{
    delete ui;
}
void ApplicationItem::setTemporaryImage(QObject* sender,const QString &email, const QByteArray &data) {
    if (this->isEmailMatched(email)) {
        QImage image;
        if (!image.loadFromData(data)) {
            qDebug() << "Failed to load image from data.";
            disconnect(sender,0,this,0);
            return;
        }
        QPixmap pixmap = QPixmap::fromImage(squareImageToCircle(image));
        setHeadImageLabelPixmap(pixmap);
        disconnect(sender,0,this,0);
    }
}
bool ApplicationItem::isEmailMatched(const QString &email) {
    return this->email == email;
}

void ApplicationItem::setHeadImageLabelPixmap(const QPixmap &pixmap) {
    ui->headImg_LB->setPixmap(pixmap);
    ui->headImg_LB->setScaledContents(true);
}
QImage ApplicationItem::squareImageToCircle(const QImage &squareImage) {
    int width = squareImage.width();
    int height = squareImage.height();
    int minSize = qMin(width, height); // 获取最小边长

    // 创建一个新的QImage，其尺寸与最小边长相同，用于绘制圆形
    QImage circleImage(minSize, minSize, QImage::Format_ARGB32);
    circleImage.fill(Qt::transparent); // 填充透明背景

    QPainter painter(&circleImage);
    painter.setRenderHint(QPainter::Antialiasing, true); // 抗锯齿

    // 计算圆形相对于中心的位置
    int centerX = minSize / 2;
    int centerY = minSize / 2;
    int radius = qMin(centerX, centerY); // 半径为圆心和图片边缘的最小距离

    // 绘制圆形区域，圆形中心在QImage的中心
    QPainterPath path;
    path.addEllipse(centerX - radius, centerY - radius, 2 * radius, 2 * radius);

    painter.setClipPath(path); // 设置裁剪路径为圆形

    // 将原始图像绘制到圆形区域内，由于设置了裁剪路径，只有圆形区域会被绘制
    // 注意：这里我们不需要对原始图像进行缩放，因为裁剪路径已经定义了绘制区域
    QRect srcRect(0, 0, width, height); // 原始图像的整个区域
    QRect targetRect(centerX - radius, centerY - radius, 2 * radius, 2 * radius); // 目标矩形与圆形路径一致
    painter.drawImage(targetRect, squareImage, srcRect); // 绘制原始图像到目标矩形区域

    painter.end(); // 结束绘制

    return circleImage;
}
void ApplicationItem::updateStatus(){
    QHBoxLayout* layout=(QHBoxLayout*)ui->widget_3->layout();
    QLabel* label=new QLabel(this);
    label->setText("已添加");
    label->setStyleSheet(R"(
        color:rgb(153, 153, 153);
    )");
    ui->agree_BT->deleteLater();
    layout->replaceWidget(ui->agree_BT,label);
}
QString ApplicationItem::getEmail(){
    return this->email;
}
QString ApplicationItem::getUsername(){
    return ui->username_LB->text();
}
void ApplicationItem::updateHeadImg(){
    if(QFile::exists(QString("./accountData/Res/%1/headImg/head.png").arg(email)))
    {
        ui->headImg_LB->setStyleSheet(QString("\
            border-radius:32px;\
            border-image: url(./accountData/Res/%1/headImg/head.png) 0 0 0 0 stretch stretch;\
        ").arg(email));
    }
}
